const { createCipheriv, createDecipheriv } = require('crypto');

class AES {
  constructor(algorithm, key, iv) {
    this.algorithm = algorithm;
    this.key = key;
    this.iv = iv;
  }
  encrypt(plaintext) {
    const cipher = createCipheriv(this.algorithm, this.key, this.iv);
    return Buffer.concat([cipher.update(plaintext), cipher.final()]).toString(
      'hex'
    );
  }
  decrypt(ciphtext) {
    const decipher = createDecipheriv(this.algorithm, this.key, this.iv);
    return Buffer.concat([
      decipher.update(ciphtext, 'hex'),
      decipher.final(),
    ]).toString();
  }
}

class AES_ECB extends AES {
  constructor(keysize, key) {
    super(`aes-${keysize}-ecb`, key, null);
  }
}

class AES_CBC extends AES {
  constructor(keysize, key, iv) {
    super(`aes-${keysize}-cbc`, key, iv);
  }
}

module.exports = {
  AES_ECB,
  AES_CBC,
};
